/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: reg.c
> Author:MOKI
> Mail:1913964829@qq.com 
> Created Time: 2024年09月13日 星期五 18时20分01秒
***************************************************/
#include "global.h"
void reg(int id_flag)
{
    char username[50] = {0}, password[20] = {0};
    int repeat_flag = 0;
    size_t rdf = 1;
    if (id_flag == 1)
    {
        Root user = {0};
        FILE *fp = fopen(ROOTS_FILE_PATH, "r+");
        if (fp == NULL)
        {
            perror("fopen");
            exit(EXIT_FAILURE); // 异常退出
        }
        else
        {
            printf("open success!\n");
        }
        while (1)
        {
            printf("请输入用户名：");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", username);
            printf("请输入密码:");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", password);
            // 检测是否重复用户名
            rdf = 1;
            while (rdf == 1)
            {
                rdf = fread(&user, sizeof(Root), 1, fp);
                if (strcmp(user.name, username) == 0)
                {
                    printf("用户名已存在");
                    repeat_flag = 1;
                    break;
                }
            }
            if (repeat_flag == 1)
            {
                printf("请重新输入\n");
                repeat_flag = 0;
                continue;
            }
            // 写入账号信息
            strcpy(user.name, username);
            strcpy(user.password, password);
            break;
        }
        fseek(fp, 0, SEEK_END);
        fwrite(&user, sizeof(Root), 1, fp);
        fclose(fp);
        printf("注册成功");
    }
    else if (id_flag == 2)
    {
        Employee user = {0};
        FILE *fp = fopen(EMPLOYEES_FILE_PATH, "r+");
        if (fp == NULL)
        {
            perror("fopen");
            exit(EXIT_FAILURE); // 异常退出
        }
        else
        {
            printf("open success!\n");
        }
        while (1)
        {
            printf("请输入用户名：");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", username);
            printf("请输入密码:");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", password);
            // 检测是否重复用户名
            rdf = 1;
            while (rdf == 1)
            {
                rdf = fread(&user, sizeof(Employee), 1, fp);
                if (strcmp(user.name, username) == 0)
                {
                    printf("用户名已存在");
                    repeat_flag = 1;
                    break;
                }
            }
            if (repeat_flag == 1)
            {
                printf("请重新输入\n");
                repeat_flag = 0;
                continue;
            }
            // 写入账号信息
            strcpy(user.name, username);
            strcpy(user.password, password);
            break;
        }
        fseek(fp, 0, SEEK_END);
        // 存储到文件
        fwrite(&user, sizeof(Root), 1, fp);
        fclose(fp);
        printf("注册成功");
    }
    else if (id_flag == 3)
    {
        Visitor user = {0};
        FILE *fp = fopen(VISITORS_FILE_PATH, "r+");
        if (fp == NULL)
        {
            perror("fopen");
            exit(EXIT_FAILURE); // 异常退出
        }
        else
        {
            printf("open success!\n");
        }
        while (1)
        {
            printf("请输入用户名：");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", username);
            printf("请输入密码:");
            setbuf(stdin, NULL); // 清除缓存区
            scanf("%s", password);
            // 检测是否重复用户名
            rdf = 1;
            while (rdf == 1)
            {
                rdf = fread(&user, sizeof(Visitor), 1, fp);
                if (strcmp(user.name, username) == 0)
                {
                    printf("用户名已存在");
                    repeat_flag = 1;
                    break;
                }
            }
            if (repeat_flag == 1)
            {
                printf("请重新输入\n");
                repeat_flag = 0;
                continue;
            }
            // 写入账号信息
            strcpy(user.name, username);
            strcpy(user.password, password);
            break;
        }
        fseek(fp, 0, SEEK_END);
        fwrite(&user, sizeof(Visitor), 1, fp);
        fclose(fp);
        printf("注册成功");
    }
}
